package logic_hotel

import (
	"APT/internal/dao"
	"APT/internal/model/entity"
	"context"
	"database/sql"
	"errors"
	"github.com/gogf/gf/v2/database/gdb"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/os/gtime"
)

// OrderExpiration 订单过期
func (s *sHotelService) OrderExpiration(ctx context.Context, OrderSn string) (err error) {
	var (
		tx                   gdb.TX
		PmsAppStay           entity.PmsAppStay
		PmsCouponTransaction *entity.PmsTransaction
	)
	if tx, err = g.DB().Begin(ctx); err != nil {
		return
	}
	defer func() {
		if err != nil {
			_ = tx.Rollback()
		} else {
			_ = tx.Commit()
		}
	}()
	if err = dao.PmsAppStay.Ctx(ctx).TX(tx).Where(g.MapStrAny{
		dao.PmsAppStay.Columns().OrderSn:     OrderSn,
		dao.PmsAppStay.Columns().OrderStatus: "WAIT_PAY",
	}).Scan(&PmsAppStay); err != nil && errors.Is(err, sql.ErrNoRows) {
		return
	}
	if g.IsEmpty(PmsAppStay) {
		err = gerror.New("该订单无需处理")
		return
	}
	// 更新当前住宿订单状态-取消
	if _, err = dao.PmsAppStay.Ctx(ctx).TX(tx).Where(g.MapStrAny{
		dao.PmsAppStay.Columns().OrderSn:     OrderSn,
		dao.PmsAppStay.Columns().OrderStatus: "WAIT_PAY",
	}).Data(g.MapStrAny{
		dao.PmsAppStay.Columns().OrderStatus: "CANCEL",
	}).Update(); err != nil {
		return
	}

	// 更新房间预约状态-取消
	if _, err = dao.PmsAppReservation.Ctx(ctx).TX(tx).Where(g.MapStrAny{
		dao.PmsAppReservation.Columns().OrderSn:     OrderSn,
		dao.PmsAppReservation.Columns().OrderStatus: "WAIT_PAY",
	}).Data(g.MapStrAny{
		dao.PmsAppReservation.Columns().Status: "cancelled",
	}).Update(); err != nil {
		return
	}

	if err = dao.PmsTransaction.Ctx(ctx).TX(tx).
		Where(dao.PmsTransaction.Columns().OrderSn, OrderSn).
		Where(dao.PmsTransaction.Columns().PayStatus, "DONE").
		Where(dao.PmsTransaction.Columns().PayType, "COUPON").
		Scan(&PmsCouponTransaction); err != nil && !errors.Is(err, sql.ErrNoRows) {
		return
	}
	if !g.IsEmpty(PmsCouponTransaction) {
		// 释放优惠券
		if _, err = dao.PmsCoupon.Ctx(ctx).TX(tx).
			Where(dao.PmsCoupon.Columns().Id, PmsCouponTransaction.CouponId).
			Update(g.MapStrAny{
				dao.PmsCoupon.Columns().State:   1,
				dao.PmsCoupon.Columns().UseTime: nil,
			}); err != nil {
			return
		}
		// 修改支付订单状态
		if _, err = dao.PmsTransaction.Ctx(ctx).TX(tx).OmitEmptyData().
			Where(dao.PmsTransaction.Columns().TransactionSn, PmsCouponTransaction.TransactionSn).
			Where(dao.PmsTransaction.Columns().PayStatus, "DONE").
			Update(entity.PmsTransaction{
				PayAmount: 0,
				PayStatus: "CANCEL",
				PayTime:   gtime.Now(),
			}); err != nil {
			return
		}
	}

	// 更新付费信息状态-取消
	if _, err = dao.PmsTransaction.Ctx(ctx).TX(tx).Where(g.MapStrAny{
		dao.PmsTransaction.Columns().OrderSn:   OrderSn,
		dao.PmsTransaction.Columns().PayStatus: "WAIT",
	}).Data(g.MapStrAny{
		dao.PmsTransaction.Columns().PayStatus: "CANCEL",
	}).Update(); err != nil {
		return
	}

	// 酒店订单取消
	if _, err = dao.PmsAppStayLog.Ctx(ctx).OmitEmptyData().Insert(&entity.PmsAppStayLog{
		OrderId:     PmsAppStay.Id,
		ActionWay:   "CANCEL",
		Remark:      "订单取消",
		OperateType: "SYSTEM",
	}); err != nil {
		return
	}
	return
}
